<?php
namespace Wechat\Controller;
use Think\Controller;
class CommonController extends Controller {

    protected $_code = 0;
    protected $_msg = '';
    protected $_data = array();
    
    public function _initialize()
    {
        header("Content-Type:text/html; charset=utf-8");

    }
    
    /**
     * @输出信息
     *
     * @param void
     * @return array
     */
    protected function outputMsg()
    {
        $msg = array();
        $msg["code"] = $this->_code;
        $msg["msg"] = $this->_msg;
        if (! empty($this->_data)) {
            $msg["data"] = $this->_data;
        }
        $this->ajaxReturn($msg);
    }
    
    /**
     * @分页
     * @author winky
     *
     * @param $model 数据对象
     * @param $field 字段
     * @param $where 条件
     * @param $order 排序
     * @param $limit 条数
     * @return array $list
     */
    protected function lists($model, $field = true, $where = array(), $order = '', $limit = 10)
    {
        $options = array();
        if (is_string($model)) {
            $model = M($model);
        }
        $OPT = new \ReflectionProperty($model, 'options');
        $OPT->setAccessible(true);
        if (! empty($order)) {
            $options['order'] = $order;
        }
        if (! empty($where)) {
            $options['where'] = $where;
        }
        $options = array_merge((array) $OPT->getValue($model), $options);
        $total = $model->where($options['where'])->count();
        $page = new \Think\Page($total, $limit);
        $p = $page->show();
        $this->assign('page', $p);
        $options['limit'] = $page->firstRow . ',' . $page->listRows;
        $model->setProperty('options', $options);
        $list = $model->field($field)->select();
        return $list;
    }
    
    
    /**
     * 404
     */
    public function _empty()
    {
        redirect('/data/404.html');
    }

    
    
    
    
    
    
    
    
    
}